unit SystemOptionsForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, DBModule,
  IB, UserUtils, Vcl.DBCtrls, DB;

type
  TfmSystemOptions = class(TForm)
    btnSave: TButton;
    btnCancel: TButton;
    PageControl1: TPageControl;
    tsCompanyDetails: TTabSheet;
    tsSalary: TTabSheet;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    txtEPFPerentEmployee: TEdit;
    txtEPFPerentEmployer: TEdit;
    GroupBox2: TGroupBox;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    lblInvoiceNo: TLabel;
    txtInvoicePrefix: TEdit;
    txtInvoiceNoLength: TEdit;
    UpDown1: TUpDown;
    Label8: TLabel;
    txtCompanyName: TEdit;
    txtRegisterNo: TEdit;
    Label18: TLabel;
    Label9: TLabel;
    txtAddress: TEdit;
    txtAddress2: TEdit;
    Label10: TLabel;
    dbcbCity: TDBLookupComboBox;
    dbcbState: TDBLookupComboBox;
    Label11: TLabel;
    Label12: TLabel;
    txtPostCode: TEdit;
    dbcbCountry: TDBLookupComboBox;
    Label13: TLabel;
    txtTel: TEdit;
    Label14: TLabel;
    Label15: TLabel;
    txtFax: TEdit;
    txtWebsite: TEdit;
    Label16: TLabel;
    txtEmail: TEdit;
    Label17: TLabel;
    tsInvoice: TTabSheet;
    Label19: TLabel;
    txtPaymentTerm: TEdit;
    UpDown2: TUpDown;
    GroupBox3: TGroupBox;
    Label20: TLabel;
    txtWorkingDays: TEdit;
    UpDown3: TUpDown;
    procedure FormCreate(Sender: TObject);
    procedure txtEPFPerentEmployeeKeyPress(Sender: TObject; var Key: Char);
    procedure txtEPFPerentEmployerKeyPress(Sender: TObject; var Key: Char);
    procedure btnSaveClick(Sender: TObject);
    procedure txtInvoicePrefixChange(Sender: TObject);
    procedure txtInvoiceNoLengthChange(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fmSystemOptions: TfmSystemOptions;

implementation

{$R *.dfm}

procedure TfmSystemOptions.btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TfmSystemOptions.btnSaveClick(Sender: TObject);
begin
  try
    with DataModule1.qrySQL do
    begin
      SQL.Clear;
      Params.Clear;
      try
        SQL.Add('update CONFIG set COMPANY_NAME=:CompanyName, ' +
                'REGISTER_NO=:RegisterNo, ' +
                'ADDRESS=:Address, ' +
                'ADDRESS2=:Address2, ' +
                'CITY=:City, ' +
                'STATE=:State, ' +
                'POSTCODE=:PostCode, ' +
                'COUNTRY=:Country, ' +
                'TEL=:Tel, ' +
                'FAX=:Fax, ' +
                'WEBSITE=:Website, ' +
                'EMAIL=:Email, ' +
                'INVOICE_PREFIX=:InvoicePrefix, ' +
                'INVOICE_NO_LENGTH=:InvoiceNoLength, ' +
                'INVOICE_PAY_TERM=:InvoicePaymentTerm, ' +
                'EPF_PER_EMPLOYEE=:EPFPerEmployee, ' +
                'EPF_PER_EMPLOYER=:EPFPerEmployer, ' +
                'WORKINGDAYS=:WorkingDays, ' +
                'Update_Date=CURRENT_TIMESTAMP');
        Params.ParamByName('CompanyName').AsString := txtCompanyName.Text;
        Params.ParamByName('RegisterNo').AsString := txtRegisterNo.Text;
        Params.ParamByName('Address').AsString := txtAddress.Text;
        Params.ParamByName('Address2').AsString := txtAddress2.Text;
        Params.ParamByName('City').AsString := dbcbCity.Text;
        Params.ParamByName('State').AsString := dbcbState.Text;
        Params.ParamByName('PostCode').AsString := txtPostCode.Text;
        Params.ParamByName('Country').AsString := dbcbCountry.Text;
        Params.ParamByName('Tel').AsString := txtTel.Text;
        Params.ParamByName('Fax').AsString := txtFax.Text;
        Params.ParamByName('Website').AsString := txtWebsite.Text;
        Params.ParamByName('Email').AsString := txtEmail.Text;
        Params.ParamByName('InvoicePrefix').AsString := txtInvoicePrefix.Text;
        Params.ParamByName('InvoiceNoLength').AsString := txtInvoiceNoLength.Text;
        Params.ParamByName('InvoicePaymentTerm').AsString := txtPaymentTerm.Text;
        Params.ParamByName('EPFPerEmployee').AsString := txtEPFPerentEmployee.Text;
        Params.ParamByName('EPFPerEmployer').AsString := txtEPFPerentEmployer.Text;
        Params.ParamByName('WorkingDays').AsString := txtWorkingDays.Text;
        if not Prepared then
          Prepare;
        ExecSQL;
        Transaction.Commit;
      except
        on E: EIBInterBaseError do
        begin
          Transaction.Rollback;
          ShowMessage(Format('IBErrorCode=%d, SQLCode=%d, Msg=%s, StackTrace=%s', [E.IBErrorCode, E.SQLCode, E.Message, E.StackTrace]));
        end;
      end;
    end;
  finally
    with DataModule1.fbConfig do
      if Active then
        Refresh
      else
        Open;
    Close;
  end;
end;

procedure TfmSystemOptions.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FreeAndNil(fmSystemOptions);
end;

procedure TfmSystemOptions.FormCreate(Sender: TObject);
begin
  with DataModule1 do
  begin
    if not fbCountry.Active then
      fbCountry.Open;
    if not fbState.Active then
      fbState.Open;
    if not fbCity.Active then
      fbCity.Open;
  end;
  with DataModule1.fbConfig do
  begin
    if not Active then
      Open;
    UpDown1.Position := FieldByName('INVOICE_NO_LENGTH').AsInteger;
    txtCompanyName.Text := FieldByName('COMPANY_NAME').AsString;
    txtInvoicePrefix.Text := FieldByName('INVOICE_PREFIX').AsString;
    lblInvoiceNo.Caption := GenerateInvoiceNo(txtInvoicePrefix.Text, UpDown1.Position, FieldByName('LAST_INVOICE_NO').AsInteger);
    txtEPFPerentEmployee.Text := FieldByName('EPF_PER_EMPLOYEE').AsString;
    txtEPFPerentEmployer.Text := FieldByName('EPF_PER_EMPLOYER').AsString;
    txtAddress.Text := FieldByName('ADDRESS').AsString;
    txtAddress2.Text := FieldByName('ADDRESS2').AsString;
    txtPostCode.Text := FieldByName('POSTCODE').AsString;
    txtTel.Text := FieldByName('TEL').AsString;
    txtFax.Text := FieldByName('FAX').AsString;
    txtWebsite.Text := FieldByName('WEBSITE').AsString;
    txtEmail.Text := FieldByName('EMAIL').AsString;
    txtRegisterNo.Text := FieldByName('REGISTER_NO').AsString;
    UpDown2.Position := FieldByName('INVOICE_PAY_TERM').AsInteger;
    UpDown3.Position := FieldByName('WORKINGDAYS').AsInteger;
    with DataModule1 do
    begin
      if dbcbCountry.ListSource.DataSet.Locate('COUNTRY', FieldByName('COUNTRY').AsString, []) then
        dbcbCountry.KeyValue := FieldByName('COUNTRY').AsString;
      if dbcbState.ListSource.DataSet.Locate('STATE', FieldByName('STATE').AsString, []) then
        dbcbState.KeyValue := FieldByName('STATE').AsString;
      if dbcbCity.ListSource.DataSet.Locate('CITY', FieldByName('CITY').AsString, []) then
        dbcbCity.KeyValue := FieldByName('CITY').AsString;
    end;
  end;
end;

procedure TfmSystemOptions.txtEPFPerentEmployeeKeyPress(Sender: TObject;
  var Key: Char);
begin
  case Key of
    '0'..'9':;
    Char(VK_BACK):;
    else
      Key := Char(0);
  end;
end;

procedure TfmSystemOptions.txtEPFPerentEmployerKeyPress(Sender: TObject;
  var Key: Char);
begin
  case Key of
    '0'..'9':;
    Char(VK_BACK):;
    else
      Key := Char(0);
  end;
end;

procedure TfmSystemOptions.txtInvoiceNoLengthChange(Sender: TObject);
begin
  lblInvoiceNo.Caption := GenerateInvoiceNo(txtInvoicePrefix.Text, UpDown1.Position,
                DataModule1.fbConfig.FieldByName('LAST_INVOICE_NO').AsInteger);
end;

procedure TfmSystemOptions.txtInvoicePrefixChange(Sender: TObject);
begin
  lblInvoiceNo.Caption := GenerateInvoiceNo(txtInvoicePrefix.Text, UpDown1.Position,
                DataModule1.fbConfig.FieldByName('LAST_INVOICE_NO').AsInteger);
end;

end.
